{%- assign pixels = storeConfig.tiktok_pixel_id | strtoarray -%}
{%- if pixels -%}
<!-- TikTok Pixel Code -->
<script>
{% for pixel in pixels %}
    !function (w, d, t) {
    w.TiktokAnalyticsObject=t;var ttq=w[t]=w[t]||[];ttq.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie"],ttq.setAndDefer=function(t,e){t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var i=0;i<ttq.methods.length;i++)ttq.setAndDefer(ttq,ttq.methods[i]);ttq.instance=function(t){for(var e=ttq._i[t]||[],n=0;n<ttq.methods.length;n++)ttq.setAndDefer(e,ttq.methods[n]);return e},ttq.load=function(e,n){var i="https://analytics.tiktok.com/i18n/pixel/events.js";ttq._i=ttq._i||{},ttq._i[e]=[],ttq._i[e]._u=i,ttq._t=ttq._t||{},ttq._t[e]=+new Date,ttq._o=ttq._o||{},ttq._o[e]=n||{};var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src=i+"?sdkid="+e+"&lib="+t;var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(o,a)};
    ttq.load('{{ pixel }}');
    ttq.page();
    }(window, document, 'ttq');
	{% endfor %}
</script>

<script>

    {%- if storeConfig.tiktok_developer_mode -%}
    {%- if template == 'product_detail' -%}
    {%- if product.id and product.variant.sku_code -%}
    if (typeof (ttq) == 'object') {
        ttq.track('ViewContent', {
            content_id: '{{ product.spu | escape }}',
    content_type: 'product',
    content_category:'{{ product.product_type | escape}}',
    quantity: 1,
    price:'{{ product.variant.price}}',
    value:'{{ product.variant.price}}',
    currency: "{{ base_currency.standard_code }}"
    });
    }
    ;
    {%- endif -%}
    {%- elsif controller == 'order' and action == 'checkout' and previous_step == 'shipping_method' and add_order == 1 -%}
    ttq.track('PlaceAnOrder');
    {%- elsif controller == 'order' and action == 'success' -%}
    {% capture pixels %}tiktok_pixels_{{order.id}}{% endcapture %}
    {% get_cache key={pixels} var_name=tiktok_pixels_status%}
    {% if tiktok_pixels_status ==0 %}
    ttq.track('CompletePayment', {
    contents: [
    {%- for product in order.products -%}
    {
    content_id: '{{ product.spu | escape}}',
        content_type: 'product',
        content_category:'{{ product.product_type | escape}}',
        quantity: '{{ product.quantity }}',
        {%- if storeConfig.tiktok_enable_ga -%}
        price: '{{ product.discount_price }}',
        {%- else -%}
        price: '{{ product.price }}',
        {%- endif -%}
        },
    {%- endfor -%}
        ],
        {%- if storeConfig.tiktok_enable_ga -%}
        value: "{{ order.current_subtotal_price | plus:order.current_coupon_price | plus:order.current_promotion_price }}",
        {%- else -%}
        value: {{order.total_price}},
        {%- endif -%}
        currency: '{{ base_currency.standard_code }}',
        });
    {%- endif -%}
    {%- elsif template == 'search_detail' and search -%}
        ttq.track('Search',{
        query:'{{ search.keywords | escape}}'
        });
    {%- endif -%}

    {%- endif -%}
</script>
<script>
    function callback_tiktok_add_to_cart(ecdata) {
            ttq.track('AddToCart', {
                content_id: ecdata['ec_data_spu'],
                content_type: 'product',
                content_name:ecdata['ec_data_name'],
                content_category:ecdata['ec_data_category'],
                quantity: ecdata['ec_data_quantity'],
                price:ecdata['ec_data_price'],
                value:ecdata['ec_data_price']*ecdata['ec_data_quantity'],
                currency: "{{ base_currency.standard_code }}"
            });
    }

    function callback_tiktok_generate_lead() {
            ttq.track('Subscribe');
    }

    function callback_tiktok_login() {
    }

    function callback_tiktok_begin_checkout() {
            ttq.track('InitiateCheckout');
    }

    function callback_tiktok_remove_from_cart(ecdata) {
    }

    function callback_tiktok_share_product(ec_data, platform) {
    }

    function callback_tiktok_add_address_information() {
    }

    function callback_tiktok_add_shipping_information() {
    }

    function callback_tiktok_add_payment_information() {
    }

    function callback_tiktok_sign_up() {
            ttq.track('CompleteRegistration');
    }
</script>

<!--End TikTok Pixel Code -->
{%- endif -%}

